package com.gdpu.project.dao.impl;


import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.gdpu.project.dao.TeacherDao;
import com.gdpu.project.vo.Teacher;


public class TeacherDaoImpl extends HibernateDaoSupport implements TeacherDao {
	private static final Log log = LogFactory.getLog(TeacherDaoImpl.class);
	// property constants
	public static final String PASSWORD = "password";
	public static final String ROLE = "role";
	public static final String NAME = "name";
	public static final String SEX = "sex";
	public static final String NATION = "nation";
	public static final String ID_CARD = "idCard";
	public static final String COLLEGE = "college";
	public static final String DEPARTMENT = "department";
	public static final String POSITION = "position";
	public static final String EDUCATION = "education";
	public static final String GRADUATE_COLLEGE = "graduateCollege";
	public static final String PROFESSION = "profession";
	public static final String SPECIALITY = "speciality";
	public static final String PHONE = "phone";
	public static final String MOBILE = "mobile";
	public static final String QQ = "qq";
	public static final String EMAIL = "email";
	public static final String FAX = "fax";

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.TeacherDao#save(com.gdpu.project.vo.Teacher)
	 */
	public void save(Teacher transientInstance) {
		log.debug("saving Teacher instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.TeacherDao#delete(com.gdpu.project.vo.Teacher)
	 */
	public void delete(Teacher persistentInstance) {
		log.debug("deleting Teacher instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.TeacherDao#findById(java.lang.String)
	 */
	public Teacher findById(String id) {
		log.debug("getting Teacher instance with id: " + id);
		try {
			Teacher instance = (Teacher) getHibernateTemplate().get(
					"com.gdpu.project.vo.Teacher", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.TeacherDao#findByExample(com.gdpu.project.vo.Teacher)
	 */
	public List<Teacher> findByExample(Teacher instance) {
		log.debug("finding Teacher instance by example");
		try {
			List<Teacher> results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.TeacherDao#findByProperty(java.lang.String, java.lang.Object)
	 */
	@SuppressWarnings("unchecked")
	public List<Teacher> findByProperty(String propertyName, Object value) {
		log.debug("finding Teacher instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Teacher as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.TeacherDao#findByPassword(java.lang.Object)
	 */
	public List<Teacher> findByPassword(Object password) {
		return findByProperty(PASSWORD, password);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.TeacherDao#findByRole(java.lang.Object)
	 */
	public List<Teacher> findByRole(Object role) {
		return findByProperty(ROLE, role);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.TeacherDao#findByName(java.lang.Object)
	 */
	public List<Teacher> findByName(Object name) {
		return findByProperty(NAME, name);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.TeacherDao#findBySex(java.lang.Object)
	 */
	public List<Teacher> findBySex(Object sex) {
		return findByProperty(SEX, sex);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.TeacherDao#findByNation(java.lang.Object)
	 */
	public List<Teacher> findByNation(Object nation) {
		return findByProperty(NATION, nation);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.TeacherDao#findByIdCard(java.lang.Object)
	 */
	public List<Teacher> findByIdCard(Object idCard) {
		return findByProperty(ID_CARD, idCard);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.TeacherDao#findByCollege(java.lang.Object)
	 */
	public List<Teacher> findByCollege(Object college) {
		return findByProperty(COLLEGE, college);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.TeacherDao#findByDepartment(java.lang.Object)
	 */
	public List<Teacher> findByDepartment(Object department) {
		return findByProperty(DEPARTMENT, department);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.TeacherDao#findByPostion(java.lang.Object)
	 */
	public List<Teacher> findByPostion(Object postion) {
		return findByProperty(POSITION, postion);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.TeacherDao#findByEducation(java.lang.Object)
	 */
	public List<Teacher> findByEducation(Object education) {
		return findByProperty(EDUCATION, education);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.TeacherDao#findByGraduateCollege(java.lang.Object)
	 */
	public List<Teacher> findByGraduateCollege(Object graduateCollege) {
		return findByProperty(GRADUATE_COLLEGE, graduateCollege);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.TeacherDao#findByProfession(java.lang.Object)
	 */
	public List<Teacher> findByProfession(Object profession) {
		return findByProperty(PROFESSION, profession);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.TeacherDao#findBySpeciality(java.lang.Object)
	 */
	public List<Teacher> findBySpeciality(Object speciality) {
		return findByProperty(SPECIALITY, speciality);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.TeacherDao#findByPhone(java.lang.Object)
	 */
	public List<Teacher> findByPhone(Object phone) {
		return findByProperty(PHONE, phone);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.TeacherDao#findByMobile(java.lang.Object)
	 */
	public List<Teacher> findByMobile(Object mobile) {
		return findByProperty(MOBILE, mobile);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.TeacherDao#findByQq(java.lang.Object)
	 */
	public List<Teacher> findByQq(Object qq) {
		return findByProperty(QQ, qq);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.TeacherDao#findByEmail(java.lang.Object)
	 */
	public List<Teacher> findByEmail(Object email) {
		return findByProperty(EMAIL, email);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.TeacherDao#findByFax(java.lang.Object)
	 */
	public List<Teacher> findByFax(Object fax) {
		return findByProperty(FAX, fax);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.TeacherDao#findAll()
	 */
	public List<Teacher> findAll() {
		log.debug("finding all Teacher instances");
		try {
			String queryString = "from Teacher";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.TeacherDao#merge(com.gdpu.project.vo.Teacher)
	 */
	public Teacher merge(Teacher detachedInstance) {
		log.debug("merging Teacher instance");
		try {
			Teacher result = (Teacher) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.TeacherDao#attachDirty(com.gdpu.project.vo.Teacher)
	 */
	public void attachDirty(Teacher instance) {
		log.debug("attaching dirty Teacher instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.dao.impl.TeacherDao#attachClean(com.gdpu.project.vo.Teacher)
	 */
	public void attachClean(Teacher instance) {
		log.debug("attaching clean Teacher instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static TeacherDao getFromApplicationContext(ApplicationContext ctx) {
		return (TeacherDao) ctx.getBean("TeacherDAO");
	}
	
	public List<Teacher> searchTeacher(String college,
			String department, String postion, String education,
			String teacherName) {
		String queryString = "from Teacher as p where p.college like '%"
			+ college + "%' " + "and p.department like '%" + department
			+ "%' and p.postion like '%" + postion
			+ "%'and p.education like '%" + education + "%'and p.name like '%" + teacherName + "%'";
	List<Teacher> list = getHibernateTemplate().find(queryString);
	return list;
	}

	@SuppressWarnings("unchecked")
	public List<String> findDistinctByDepartment() {
			String hql = "select distinct department from Teacher";
			List<String> list = getHibernateTemplate().find(hql);
			return list;	
	}
	public List<String> findDistinctByEducation() {
		String hql = "select distinct education from Teacher";
		List<String> list = getHibernateTemplate().find(hql);
		return list;	
	}
	public List<String> findDistinctByPostion() {
		String hql = "select distinct position from Teacher";
		List<String> list = getHibernateTemplate().find(hql);
		return list;	
	}


	@SuppressWarnings("unchecked")
	public List<Teacher> searchTeacher(String college,
			String department, String position, String education,String cataloge,
			String teacherId){
		StringBuffer hql = new StringBuffer();
		List l = new ArrayList<Object>();
		Object[] arg;
		hql.append("from Teacher as t where 1=1");
		if(teacherId!=null&&!"".equals(teacherId)){
			hql.append(" and t.teacherId=?");
			l.add(teacherId);
		}
		if(college!=null&&!"".equals(college)){
			hql.append(" and t.college=?");
			l.add(college);
		}
		if(department!=null&&!"".equals(department)){
			hql.append(" and t.department=?");
			l.add(department);
		}
		if(position!=null&&!"".equals(position)){
			hql.append(" and t.position=?");
			l.add(position);
		}
		if(education!=null&&!"".equals(education)){
			hql.append(" and t.education=?");
			l.add(education);
		}
		if(!"".equals(cataloge)&&cataloge!=null){
			hql.append(" and t.cataloge=?");
			l.add(cataloge);
		}
		arg = (Object[]) l.toArray(new Object[]{});
		
		/*Object[] args={college,department,postion,education,teacherId};
		System.out.println(college+department+postion+education+teacherId+cataloge);
		String hql="from Teacher as t where (t.college like ?) and t.department like ?"
				+" and t.postion like ? and t.education like ? and t.teacherId like ?";*/
		
		List<Teacher> list=(List<Teacher>)getHibernateTemplate().find(hql.toString(), arg);
		return list;

		
	}
	
}